package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.actions.OpenBindedDiagram;
import edu.pku.sei.transformation.editor.controller.policies.NodeComponentEditPolicy;
import edu.pku.sei.transformation.editor.controller.policies.TransformationNodeEditPolicy;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.Transformation;
import edu.pku.sei.transformation.editor.views.figures.TransformationFigure;

public class TransformationEditpart extends AbstractNodeEditPart {

	public TransformationEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		return null;
	}

	@Override
	protected IFigure createFigure() {
		TransformationFigure figure = new TransformationFigure();
		Transformation trans = (Transformation) getNode().getModelElement();
		figure.setName(trans.getName());
		return figure;
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new TransformationNodeEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new NodeComponentEditPolicy());
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		Transformation trans = (Transformation) getNode().getModelElement();
		TransformationFigure figure = (TransformationFigure) getFigure();
		String propertyName = event.getPropertyName();

		if (ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)) {
			figure.setName(event.getNewValue().toString());
		} else {
			super.handlePropertyChanged(event);
		}
	}

	@Override
	protected void refreshVisuals() {
		Transformation trans = (Transformation) getNode().getModelElement();
		TransformationFigure figure = (TransformationFigure) getFigure();
		figure.setName(trans.getName());
		super.refreshVisuals();
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		if (anchor == null)
			anchor = new ChopboxAnchor(getFigure());
		return anchor;
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		if (anchor == null)
			anchor = new ChopboxAnchor(getFigure());
		return anchor;
	}

	@Override
	public void performRequest(Request request) {
		if (request.getType() == RequestConstants.REQ_OPEN) {
			OpenBindedDiagram action = new OpenBindedDiagram(
					(Transformation) this.getNode().getModelElement());
			action.run();
		} else
			super.performRequest(request);
	}

}
